home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 17 controls / databindingdemo / listcomboboxbindingform.vb < prev    next >
Text File  |  2002-03-16  |  11KB  |  256 lines

  1. Public Class ListComboBoxBindingForm
  2.     Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.  
  9.         'This call is required by the Windows Form Designer.
  10.         InitializeComponent()
  11.  
  12.         'Add any initialization after the InitializeComponent() call
  13.  
  14.     End Sub
  15.  
  16.     'Form overrides dispose to clean up the component list.
  17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18.         If disposing Then
  19.             If Not (components Is Nothing) Then
  20.                 components.Dispose()
  21.             End If
  22.         End If
  23.         MyBase.Dispose(disposing)
  24.     End Sub
  25.     Friend WithEvents Label1 As System.Windows.Forms.Label
  26.     Friend WithEvents txtFirstName As System.Windows.Forms.TextBox
  27.     Friend WithEvents lblRecord As System.Windows.Forms.Label
  28.     Friend WithEvents btnPrevious As System.Windows.Forms.Button
  29.     Friend WithEvents btnNext As System.Windows.Forms.Button
  30.     Friend WithEvents btnLast As System.Windows.Forms.Button
  31.     Friend WithEvents btnFirst As System.Windows.Forms.Button
  32.     Friend WithEvents Label2 As System.Windows.Forms.Label
  33.     Friend WithEvents txtLastName As System.Windows.Forms.TextBox
  34.     Friend WithEvents Label3 As System.Windows.Forms.Label
  35.     Friend WithEvents txtBirthDate As System.Windows.Forms.TextBox
  36.     Friend WithEvents chkPartTime As System.Windows.Forms.CheckBox
  37.     Friend WithEvents Label4 As System.Windows.Forms.Label
  38.     Friend WithEvents cboDepartments As System.Windows.Forms.ComboBox
  39.     
  40.     'Required by the Windows Form Designer
  41.     Private components As System.ComponentModel.Container
  42.  
  43.     'NOTE: The following procedure is required by the Windows Form Designer
  44.     'It can be modified using the Windows Form Designer.  
  45.     'Do not modify it using the code editor.
  46.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  47.         Me.txtBirthDate = New System.Windows.Forms.TextBox()
  48.         Me.Label2 = New System.Windows.Forms.Label()
  49.         Me.Label3 = New System.Windows.Forms.Label()
  50.         Me.btnLast = New System.Windows.Forms.Button()
  51.         Me.lblRecord = New System.Windows.Forms.Label()
  52.         Me.btnNext = New System.Windows.Forms.Button()
  53.         Me.cboDepartments = New System.Windows.Forms.ComboBox()
  54.         Me.chkPartTime = New System.Windows.Forms.CheckBox()
  55.         Me.txtFirstName = New System.Windows.Forms.TextBox()
  56.         Me.btnPrevious = New System.Windows.Forms.Button()
  57.         Me.Label4 = New System.Windows.Forms.Label()
  58.         Me.txtLastName = New System.Windows.Forms.TextBox()
  59.         Me.Label1 = New System.Windows.Forms.Label()
  60.         Me.btnFirst = New System.Windows.Forms.Button()
  61.         Me.SuspendLayout()
  62.         '
  63.         'txtBirthDate
  64.         '
  65.         Me.txtBirthDate.Location = New System.Drawing.Point(16, 112)
  66.         Me.txtBirthDate.Name = "txtBirthDate"
  67.         Me.txtBirthDate.Size = New System.Drawing.Size(192, 24)
  68.         Me.txtBirthDate.TabIndex = 5
  69.         Me.txtBirthDate.Text = ""
  70.         '
  71.         'Label2
  72.         '
  73.         Me.Label2.Location = New System.Drawing.Point(232, 24)
  74.         Me.Label2.Name = "Label2"
  75.         Me.Label2.Size = New System.Drawing.Size(168, 24)
  76.         Me.Label2.TabIndex = 2
  77.         Me.Label2.Text = "Last Name"
  78.         '
  79.         'Label3
  80.         '
  81.         Me.Label3.Location = New System.Drawing.Point(16, 88)
  82.         Me.Label3.Name = "Label3"
  83.         Me.Label3.Size = New System.Drawing.Size(168, 24)
  84.         Me.Label3.TabIndex = 4
  85.         Me.Label3.Text = "Birth Date"
  86.         '
  87.         'btnLast
  88.         '
  89.         Me.btnLast.Location = New System.Drawing.Point(328, 224)
  90.         Me.btnLast.Name = "btnLast"
  91.         Me.btnLast.Size = New System.Drawing.Size(48, 32)
  92.         Me.btnLast.TabIndex = 11
  93.         Me.btnLast.Text = ">>"
  94.         '
  95.         'lblRecord
  96.         '
  97.         Me.lblRecord.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  98.         Me.lblRecord.Location = New System.Drawing.Point(192, 224)
  99.         Me.lblRecord.Name = "lblRecord"
  100.         Me.lblRecord.Size = New System.Drawing.Size(88, 32)
  101.         Me.lblRecord.TabIndex = 9
  102.         Me.lblRecord.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
  103.         '
  104.         'btnNext
  105.         '
  106.         Me.btnNext.Location = New System.Drawing.Point(280, 224)
  107.         Me.btnNext.Name = "btnNext"
  108.         Me.btnNext.Size = New System.Drawing.Size(48, 32)
  109.         Me.btnNext.TabIndex = 10
  110.         Me.btnNext.Text = ">"
  111.         '
  112.         'cboDepartments
  113.         '
  114.         Me.cboDepartments.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
  115.         Me.cboDepartments.DropDownWidth = 192
  116.         Me.cboDepartments.Location = New System.Drawing.Point(16, 176)
  117.         Me.cboDepartments.Name = "cboDepartments"
  118.         Me.cboDepartments.Size = New System.Drawing.Size(192, 26)
  119.         Me.cboDepartments.TabIndex = 13
  120.         '
  121.         'chkPartTime
  122.         '
  123.         Me.chkPartTime.Location = New System.Drawing.Point(232, 112)
  124.         Me.chkPartTime.Name = "chkPartTime"
  125.         Me.chkPartTime.Size = New System.Drawing.Size(176, 16)
  126.         Me.chkPartTime.TabIndex = 6
  127.         Me.chkPartTime.Text = "PartTime Employee"
  128.         '
  129.         'txtFirstName
  130.         '
  131.         Me.txtFirstName.Location = New System.Drawing.Point(16, 48)
  132.         Me.txtFirstName.Name = "txtFirstName"
  133.         Me.txtFirstName.Size = New System.Drawing.Size(192, 24)
  134.         Me.txtFirstName.TabIndex = 1
  135.         Me.txtFirstName.Text = ""
  136.         '
  137.         'btnPrevious
  138.         '
  139.         Me.btnPrevious.Location = New System.Drawing.Point(144, 224)
  140.         Me.btnPrevious.Name = "btnPrevious"
  141.         Me.btnPrevious.Size = New System.Drawing.Size(48, 32)
  142.         Me.btnPrevious.TabIndex = 8
  143.         Me.btnPrevious.Text = "<"
  144.         '
  145.         'Label4
  146.         '
  147.         Me.Label4.Location = New System.Drawing.Point(16, 152)
  148.         Me.Label4.Name = "Label4"
  149.         Me.Label4.Size = New System.Drawing.Size(160, 24)
  150.         Me.Label4.TabIndex = 12
  151.         Me.Label4.Text = "Department"
  152.         '
  153.         'txtLastName
  154.         '
  155.         Me.txtLastName.Location = New System.Drawing.Point(232, 48)
  156.         Me.txtLastName.Name = "txtLastName"
  157.         Me.txtLastName.Size = New System.Drawing.Size(192, 24)
  158.         Me.txtLastName.TabIndex = 3
  159.         Me.txtLastName.Text = ""
  160.         '
  161.         'Label1
  162.         '
  163.         Me.Label1.Location = New System.Drawing.Point(16, 24)
  164.         Me.Label1.Name = "Label1"
  165.         Me.Label1.Size = New System.Drawing.Size(168, 24)
  166.         Me.Label1.TabIndex = 0
  167.         Me.Label1.Text = "First Name"
  168.         '
  169.         'btnFirst
  170.         '
  171.         Me.btnFirst.Location = New System.Drawing.Point(96, 224)
  172.         Me.btnFirst.Name = "btnFirst"
  173.         Me.btnFirst.Size = New System.Drawing.Size(48, 32)
  174.         Me.btnFirst.TabIndex = 7
  175.         Me.btnFirst.Text = "<<"
  176.         '
  177.         'ListComboBoxBindingForm
  178.         '
  179.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  180.         Me.ClientSize = New System.Drawing.Size(456, 269)
  181.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cboDepartments, Me.Label4, Me.chkPartTime, Me.Label3, Me.txtBirthDate, Me.Label2, Me.txtLastName, Me.lblRecord, Me.btnLast, Me.btnNext, Me.btnPrevious, Me.btnFirst, Me.txtFirstName, Me.Label1})
  182.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  183.         Me.Name = "ListComboBoxBindingForm"
  184.         Me.Text = "Bound ComboBox control"
  185.         Me.ResumeLayout(False)
  186.  
  187.     End Sub
  188.  
  189. #End Region
  190.  
  191.     ' create two arrays of objects that will work as data sources
  192.  
  193.     Dim employees() As Employee = {New Employee("Joe", "Doe", #1/3/1960#, , 1), _
  194.         New Employee("Robert", "Smith", #11/23/1962#, , 2), _
  195.         New Employee("Ann", "Ross", #2/5/1965#, True, 3)}
  196.  
  197.     Dim departments() As Department = {New Department("Sales", 1), _
  198.         New Department("Tech Support", 2), New Department("Marketing", 3)}
  199.  
  200.     Dim WithEvents bmb As BindingManagerBase
  201.  
  202.     ' initialize binding when the form loads
  203.  
  204.     Private Sub SimpleBindingForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  205.         InitializeBinding()
  206.     End Sub
  207.  
  208.     Sub InitializeBinding()
  209.         ' Load the Departments in the combobox control.
  210.         cboDepartments.DisplayMember = "Name"
  211.         cboDepartments.ValueMember = "ID"
  212.         cboDepartments.DataSource = departments
  213.  
  214.         ' Create the Binding object for each bound field.
  215.         txtFirstName.DataBindings.Add("Text", employees, "FirstName")
  216.         txtLastName.DataBindings.Add("Text", employees, "LastName")
  217.         txtBirthDate.DataBindings.Add("Text", employees, "BirthDate")
  218.         chkPartTime.DataBindings.Add("Checked", employees, "PartTimeEmployee")
  219.         cboDepartments.DataBindings.Add("SelectedValue", employees, "DepartmentID")
  220.  
  221.         ' Save a reference to the BindingManagerBase object.
  222.         bmb = Me.BindingContext(employees)
  223.         ' Force a refresh of buttons
  224.         bmb.Position = bmb.Count
  225.         bmb.Position = 0
  226.     End Sub
  227.  
  228.     ' navigational buttons
  229.  
  230.     Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
  231.         bmb.Position = 0
  232.     End Sub
  233.  
  234.     Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
  235.         bmb.Position -= 1
  236.     End Sub
  237.  
  238.     Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
  239.         bmb.Position += 1
  240.     End Sub
  241.  
  242.     Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
  243.         bmb.Position = bmb.Count - 1
  244.     End Sub
  245.  
  246.     ' Display current position in label.
  247.     Private Sub bmb_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles bmb.PositionChanged
  248.         lblRecord.Text = String.Format("{0} of {1}", bmb.Position + 1, bmb.Count)
  249.         ' Enable/disable buttons.
  250.         btnFirst.Enabled = (bmb.Position > 0)
  251.         btnPrevious.Enabled = (bmb.Position > 0)
  252.         btnNext.Enabled = (bmb.Position < bmb.Count - 1)
  253.         btnLast.Enabled = (bmb.Position < bmb.Count - 1)
  254.     End Sub
  255. End Class
  256.